package com.hanbang.chu_xing_gao_zhao.view_find_classroom;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hanbang.chu_xing_gao_zhao.base.BaseDataActivity;
import com.hanbang.chu_xing_gao_zhao.base.BaseFragmentActivity;
import com.hanbang.chu_xing_gao_zhao.myview.NoScrollViewPager;
import com.hanbang.cxgz_2.R;

import java.util.ArrayList;

/**
 * 大厨课堂，掌柜课堂，学习资料，展示的activity
 *
 * @author yang
 */
public class FindClassroomFragmentActivity extends BaseDataActivity implements OnClickListener {
    private NoScrollViewPager viewPager;
    private FragmentActivityAdapter adapter;
    private ArrayList<Fragment> fragments;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_classroom_fragment_activity);

        initView();
        setOnClickListener();


    }


    /**
     * 初始化view
     */
    private void initView() {
        Intent intent = getIntent();
        //接收传过来的类型，判断的chef,还是manager,跳转过来的。
        String type = intent.getStringExtra("type");


        setBackArrows(findViewById(R.id.bar_top_conver_classify_back));
        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1)).setBackgroundResource(R.color.white);


        fragments = new ArrayList<Fragment>();
        viewPager = (NoScrollViewPager) findViewById(R.id.find_classroom_fragment_activity_viewPage);
        viewPager.setNoScroll(false);


        if (type.equals("chef")) {
            setTitleMore(View.VISIBLE, View.VISIBLE, "大厨课堂", "掌柜课堂", "学习资料", null);
            fragments.add(new FindClassroomChefAndManagerFragment(1));
            fragments.add(new FindClassroomChefAndManagerFragment(2));
        } else {
            setTitleMore(View.VISIBLE, View.VISIBLE, "掌柜课堂", "大厨课堂", "学习资料", null);
            fragments.add(new FindClassroomChefAndManagerFragment(2));
            fragments.add(new FindClassroomChefAndManagerFragment(1));
        }
        fragments.add(new FindClassroomDataFragment());

        adapter = new FragmentActivityAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);


    }


    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1)).setOnClickListener(this);
        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title2)).setOnClickListener(this);
        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title3)).setOnClickListener(this);


        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                switch (position) {
                    //大厨课堂
                    case 0:
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1)).setBackgroundResource(R.color.white);
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title2)).setBackgroundResource(R.color.yellow);
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title3)).setBackgroundResource(R.color.yellow);
                        viewPager.setCurrentItem(0);
                        break;
                    //掌柜课堂
                    case 1:
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title2)).setBackgroundResource(R.color.white);
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1)).setBackgroundResource(R.color.yellow);
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title3)).setBackgroundResource(R.color.yellow);
                        viewPager.setCurrentItem(1);
                        break;
                    //学习资料
                    case 2:
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title3)).setBackgroundResource(R.color.white);
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1)).setBackgroundResource(R.color.yellow);
                        ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title2)).setBackgroundResource(R.color.yellow);
                        viewPager.setCurrentItem(2);
                        break;

                    default:

                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();

        switch (v.getId()) {
            //大厨课堂
            case R.id.tv_bar_top_conver_classify_title1:
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1)).setBackgroundResource(R.color.white);
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title2)).setBackgroundResource(R.color.yellow);
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title3)).setBackgroundResource(R.color.yellow);
                viewPager.setCurrentItem(0);
                break;
            //掌柜课堂
            case R.id.tv_bar_top_conver_classify_title2:
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title2)).setBackgroundResource(R.color.white);
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1)).setBackgroundResource(R.color.yellow);
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title3)).setBackgroundResource(R.color.yellow);
                viewPager.setCurrentItem(1);
                break;
            //学习资料
            case R.id.tv_bar_top_conver_classify_title3:
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title3)).setBackgroundResource(R.color.white);
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1)).setBackgroundResource(R.color.yellow);
                ((TextView) findViewById(R.id.tv_bar_top_conver_classify_title2)).setBackgroundResource(R.color.yellow);
                viewPager.setCurrentItem(2);
                break;

            default:

                break;
        }


    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * viewPage 适配器
     */
    public class FragmentActivityAdapter extends FragmentStatePagerAdapter {
        private ArrayList<Fragment> fragments;
        FragmentManager fm;

        public FragmentActivityAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            if (fragments == null) {

                this.fragments = list;
                this.fm = fm;

            } else {
                this.fragments.clear();
                fragments = new ArrayList<Fragment>();
            }
        }


        @Override
        public Fragment getItem(int position) {

            return fragments.get(position);
        }

        @Override
        public int getCount() {

            return fragments.size();
        }


        @Override
        public boolean isViewFromObject(View view, Object object) {
            return super.isViewFromObject(view, object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);

        }


    }
}


